package leetcode.D300.T205;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

class Solution {
    public boolean isIsomorphic(String s, String t) {
        if(s.length() != t.length()) return false;
        Map<Character, Character> map = new HashMap<>();
        Set<Character> set = new HashSet<>();
        for(int i=0; i<s.length(); ++i) {
            char a = s.charAt(i), b = t.charAt(i);
            if(set.contains(b)) {
                if(!map.containsKey(a) || map.get(a) != b) {
                    return false;
                }
            } else {
                if(map.containsKey(a)) {
                    return false;
                }
                set.add(b);
                map.put(a, b);
            }
        }
        return true;
    }
}